<template>
  <div class="home-page">
    <div class="banner">
      <div class="container">
        <h1 class="logo-font">
          conduit
        </h1>
        <p>A place to share your knowledge.</p>
      </div>
    </div>

    <div class="container page">
      <div class="row">
        <div class="col-md-9">
          <div class="feed-toggle">
            <ul class="nav nav-pills outline-active">
              <li class="nav-item">
                <a class="nav-link disabled" href="">Your Feed</a>
              </li>
              <li class="nav-item">
                <a class="nav-link active" href="">Global Feed</a>
              </li>
            </ul>
          </div>

          <div v-for="article in articles" :key="article.slug" class="article-preview">
            <div class="article-meta">
              <nuxt-link
                :to="{
                  name:'profile',
                  params:{
                    username:article.author.username
                  }
                }"
              >
                <img :src="article.author.image">
              </nuxt-link>
              <div class="info">
                <nuxt-link
                  class="author"
                  :to="{
                    name:'profile',
                    params:{
                      username:article.author.username
                    }
                  }"
                >
                  Eric Simons
                </nuxt-link>
                <span class="date">{{ article.createdAt }}</span>
              </div>
              <button class="btn btn-outline-primary btn-sm pull-xs-right">
                <i class="ion-heart" /> {{ article.favoritesCount }}
              </button>
            </div>
            <nuxt-link
              :to="{
                name:'article',
                params:{
                  title:article.title
                }
              }"
              class="preview-link"
            >
              <h1>{{ article.title }}</h1>
              <p>{{ article.description }}</p>
              <span>Read more...</span>
            </nuxt-link>
          </div>
        </div>

        <div class="col-md-3">
          <div class="sidebar">
            <p>Popular Tags</p>

            <div class="tag-list">
              <a href="" class="tag-pill tag-default">programming</a>
              <a href="" class="tag-pill tag-default">javascript</a>
              <a href="" class="tag-pill tag-default">emberjs</a>
              <a href="" class="tag-pill tag-default">angularjs</a>
              <a href="" class="tag-pill tag-default">react</a>
              <a href="" class="tag-pill tag-default">mean</a>
              <a href="" class="tag-pill tag-default">node</a>
              <a href="" class="tag-pill tag-default">rails</a>
            </div>
          </div>
        </div>
        <nav>
          <ul class="pagination">
            <li
              v-for="item in totalPage"
              :key="item"
              class="page-item"
              :class="{
                active:item === page
              }"
            >
              <nuxt-link
                class="page-link"
                :to="{
                  name:'home',
                  query:{
                    page:item
                  }
                }"
              >
                {{ item }}
              </nuxt-link>
            </li>
          </ul>
        </nav>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { articlesList } from '@/api/articles'

export default Vue.extend({
  async asyncData ({ query }) {
    const limit = 20
    // @ts-ignore: Unreachable code error
    const page = Number.parseInt(query.page || 1)
    const data = await articlesList({ limit, offset: (page - 1) * limit })
    return {
      // @ts-ignore: Unreachable code error
      articles: data.articles,
      // @ts-ignore: Unreachable code error
      articlesCount: data.articlesCount,
      limit,
      page
    }
  },
  watchQuery: ['page'],
  computed: {
    totalPage () {
      // @ts-ignore: Unreachable code error
      return Math.ceil(this.articlesCount / this.limit)
    }
  }
})
</script>

<style>

</style>
